package com.tcxhb.job.admin.controller;

import com.tcxhb.job.admin.convert.JobInstanceConvert;
import com.tcxhb.job.dao.dataobject.JobInfoDO;
import com.tcxhb.job.dao.dataobject.JobInstanceDO;
import com.tcxhb.job.dao.dataobject.query.JobInstanceQuery;
import com.tcxhb.job.admin.model.request.query.JobInstanceQueryReq;
import com.tcxhb.job.admin.model.response.JobInstanceVO;
import com.tcxhb.job.admin.utils.ContextHolder;
import com.tcxhb.job.core.schedule.execute.JobRunExecutor;
import com.tcxhb.job.core.service.job.JobInfoService;
import com.tcxhb.job.core.service.job.JobInstanceService;
import com.tcxhb.job.common.constants.CommonErrorCode;
import com.tcxhb.job.common.model.MiscResult;
import com.tcxhb.job.common.model.PageResponse;
import com.tcxhb.job.common.request.CommonIdDeleteReq;
import com.tcxhb.job.common.request.CommonIdQueryReq;
import com.tcxhb.job.common.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * author:auto.generator
 * time: 2023-11-05
 */
@RestController
@RequestMapping("/job/instance")
public class JobInstanceController extends BaseController {

    @Autowired
    private JobInstanceConvert convert;
    @Autowired
    private JobInstanceService jobInstanceService;
    @Autowired
    private JobInfoService jobInfoService;
    @Autowired
    private JobRunExecutor executor;

    @PostMapping(value = "/page", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询")
    public MiscResult<PageResponse<JobInstanceVO>> page(@RequestBody JobInstanceQueryReq req) {
        fillAppName(req);
        JobInstanceQuery query = convert.defQuery(req);
        PageResponse<JobInstanceDO> response = jobInstanceService.page(query);
        PageResponse<JobInstanceVO> result = ConvertUtils.page(response, convert::do2VO);
        fillJobName(result.getList());
        return MiscResult.suc(result);
    }


    @PostMapping(value = "/queryById", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<JobInstanceVO> queryById(@RequestBody CommonIdQueryReq req) {
        JobInstanceDO relationDO = jobInstanceService.queryById(req.getId());
        JobInstanceVO vo = convert.do2VO(relationDO);
        return MiscResult.suc(vo);
    }

    @PostMapping(value = "/deleteById", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<String> deleteById(@RequestBody CommonIdDeleteReq req) {
        Boolean result = jobInstanceService.deleteById(req.getId());
        if (Boolean.TRUE.equals(result)) {
            return MiscResult.suc("操作成功");
        }
        return MiscResult.err(CommonErrorCode.BAD_REQUEST);
    }

    @PostMapping(value = "/reRun", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<String> reRun(@RequestBody CommonIdQueryReq req) {
        return executor.retry(req.getId(), true);
    }

    /**
     * 填充名字
     *
     * @param voList
     */
    private void fillJobName(List<JobInstanceVO> voList) {
        if (CollectionUtils.isEmpty(voList)) {
            return;
        }
        Set<Long> ids = voList.stream().map(JobInstanceVO::getJobId).collect(Collectors.toSet());
        Map<Long, JobInfoDO> maps = jobInfoService.queryByIds(ids.stream().collect(Collectors.toList()));
        for (JobInstanceVO vo : voList) {
            JobInfoDO jobDO = maps.get(vo.getJobId());
            if (jobDO != null) {
                vo.setJobName(jobDO.getJobName());
            }
        }
    }
}
